package com.fz.us.admin.company.dao.impl;

import com.fz.us.admin.base.bean.BaseEnumManager;
import com.fz.us.admin.base.dao.impl.BaseDaoImpl;
import org.apache.commons.collections.CollectionUtils;
import com.fz.us.admin.company.bean.EnumManage;
import com.fz.us.admin.company.entity.*;
import com.fz.us.admin.company.dao.DutyDao;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

/**
 * Dao 实现类 - 职位
 * ============================================================================
 * 版权所有 2016 。
 *
 * @author
 * @version 0.1 2016-07-26
 * ============================================================================
 */
@Repository
public class DutyDaoImpl extends BaseDaoImpl<Duty,String> implements DutyDao{

    @Override
    public boolean hasPower(Power power) {
        Assert.notNull(power, "power is required");
        String hql = "select count(*) from " + Duty.class.getName() + " model where model.power = :power and ( model.state = :state1 or model.state = :state2 )";
        return ((Long) getSession().createQuery(hql).setParameter("power", power).setParameter("state1", BaseEnumManager.StateEnum.Enable).setParameter("state2", BaseEnumManager.StateEnum.Disenable).uniqueResult()) > 0;
    }



    @Override
    public boolean hasDepartment(Department department) {
        Assert.notNull(department, "user is required");
        String hql = "select count(*) from " + Duty.class.getName() + " model where model.power.department = :department and ( model.state = :state1 or model.state = :state2 )";
        return ((Long) getSession().createQuery(hql).setParameter("department", department).setParameter("state1", BaseEnumManager.StateEnum.Enable).setParameter("state2", BaseEnumManager.StateEnum.Disenable).uniqueResult()) > 0;
    }

    @Override
    public boolean hasMainDuty(User user) {
        Assert.notNull(user, "user is required");
        String hql = "select count(*) from " + Duty.class.getName() + " model where model.user = :user and model.ifMain = :ifMain and ( model.state = :state1 or model.state = :state2 )";
        return ((Long) getSession().createQuery(hql).setParameter("user", user).setParameter("ifMain", 1).setParameter("state1", BaseEnumManager.StateEnum.Enable).setParameter("state2", BaseEnumManager.StateEnum.Disenable).uniqueResult()) > 0;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Duty> getUserDutyList(User user) {
        Assert.notNull(user, "user is required");
        String hql = "from "+Duty.class.getName()+" model where model.user = :user and model.state = :state";
        return getSession().createQuery(hql).setParameter("user", user).setParameter("state", BaseEnumManager.StateEnum.Enable).list();
    }

    @SuppressWarnings("unchecked")
    @Override
    public Duty getUserDefaultDuty(User user) {
        Assert.notNull(user, "user is required");
        String hql = "from "+Duty.class.getName()+" model where model.user = :user and model.ifMain = :ifMain and model.state = :state";
        List<Duty> list =  getSession().createQuery(hql).setParameter("user", user).setParameter("ifMain", 1).setParameter("state", BaseEnumManager.StateEnum.Enable).list();
        if(list!=null&&list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Duty> getDutyListByPower(Power power) {
        Assert.notNull(power, "power is required");
        String hql = "from  "+Duty.class.getName()+" model where model.power = :power and model.state = :state";
        return getSession().createQuery(hql).setParameter("power", power).setParameter("state", BaseEnumManager.StateEnum.Enable).list();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Duty> getDutyListByDepartment(Department department) {
        Assert.notNull(department, "department is required");
        String hql = "from  "+Duty.class.getName()+" model where model.power.department = :department and model.state = :state";
        return getSession().createQuery(hql).setParameter("department", department).setParameter("state", BaseEnumManager.StateEnum.Enable).list();
    }



    @SuppressWarnings("unchecked")
    @Override
    public List<Duty> getDutyListByPost(Post post) {
        Assert.notNull(post, "department is required");
        String hql = "from  "+Duty.class.getName()+" model where model.power.post = :post and model.state = :state";
        return getSession().createQuery(hql).setParameter("post", post).setParameter("state", BaseEnumManager.StateEnum.Enable).list();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<User> getPowerUserList(Power power) {
        Assert.notNull(power, "power is required");
        String hql = "from  "+Duty.class.getName()+" model where model.power = :power and model.state = :state";
        List<Duty> list = getSession().createQuery(hql).setParameter("power", power).setParameter("state", BaseEnumManager.StateEnum.Enable).list();
        List<User> usersList = new ArrayList<User>();
        if(list!=null&&list.size()>0){
            for(Duty duty:list){
                if(duty.getUser()!=null&&!usersList.contains(duty.getUser())){
                    usersList.add(duty.getUser());
                }

            }
        }
        return usersList;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<User> getDepartmentUserList(Department department) {
        Assert.notNull(department, "department is required");
        String hql = "from  "+Duty.class.getName()+" model where model.power.department = :department and model.state = :state";
        List<Duty> list = getSession().createQuery(hql).setParameter("department", department).setParameter("state", BaseEnumManager.StateEnum.Enable).list();
        List<User> usersList = new ArrayList<User>();
        if(list!=null&&list.size()>0){
            for(Duty duty:list){
                if(duty.getUser()!=null&&!usersList.contains(duty.getUser())){
                    usersList.add(duty.getUser());
                }

            }
        }
        return usersList;
    }



    @SuppressWarnings("unchecked")
    @Override
    public List<User> getPostUserList(Post post) {
        Assert.notNull(post, "department is required");
        String hql = "from  "+Duty.class.getName()+" model where model.power.post = :post and model.state = :state";
        List<Duty> list = getSession().createQuery(hql).setParameter("post", post).setParameter("state", BaseEnumManager.StateEnum.Enable).list();
        List<User> usersList = new ArrayList<User>();
        if(list!=null&&list.size()>0){
            for(Duty duty:list){
                if(duty.getUser()!=null&&!usersList.contains(duty.getUser())){
                    usersList.add(duty.getUser());
                }

            }
        }
        return usersList;
    }

    @Override
    public Duty getPrincipalDuty(Department department) {
        Assert.notNull(department, "department is required");
        String hql = "from Duty duty where duty.power.department.id = ? and duty.dutyState = ? and duty.state = ? ";
        List<Duty> list = getSession().createQuery(hql).setParameter(0, department.getId()).setParameter(1, EnumManage.DutyState.Principal).setParameter(2, BaseEnumManager.StateEnum.Enable).list();
        if(list!=null&&list.size()>0){
            Duty duty = list.get(0);
            if(duty.getUser()!=null){
                return duty;
            }
        }
        return null;
    }

    @Override
    public Duty getDefaultDuty(User user) {
        Assert.notNull(user, "user is required");
        String hql = "from Duty duty where duty.user.id = ? and duty.ifMain = 1 and duty.state = ?";
        List<Duty> list = getSession().createQuery(hql).setParameter(0, user.getId()).setParameter(1, BaseEnumManager.StateEnum.Enable).list();
        if(list!=null&&list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<Department> getDepartments(User user, Company company) {
        Assert.notNull(user, "user is required");
        String hql = "from Duty duty where duty.user.id = ? and duty.state = ?  and duty.company.id=?";
        List<Duty> list = getSession().createQuery(hql).setParameter(0, user.getId()).setParameter(1, BaseEnumManager.StateEnum.Enable).setParameter(2,company.getId()).list();
        List<Department> dList = new ArrayList<Department>();
        if(CollectionUtils.isEmpty(list) || list.size()==0){
            return  dList;
        }
        for(Duty duty:list){
            if(duty.getPower() != null){
                dList.add(duty.getPower().getDepartment());
            }
        }
        return dList;
    }

    @Override
    public List<Department> getDepartments(User user, Post post) {
        Assert.notNull(user, "user is required");
        Assert.notNull(post, "post is required");
        String hql = "from Duty duty where duty.user.id = ? and duty.power.post.id = ? and state = ? ";
        List<Duty> list = getSession().createQuery(hql).setParameter(0, user.getId()).setParameter(1, post.getId()).setParameter(2, BaseEnumManager.StateEnum.Enable).list();
        List<Department> dList = new ArrayList<Department>();
        if(CollectionUtils.isEmpty(list) || list.size()>0){
            return  dList;
        }
        for(Duty duty:list){
            if(duty.getPower() != null){
                dList.add(duty.getPower().getDepartment());
            }
        }
        return dList;
    }

    @Override
    public List<Duty> getDutys(User user) {
        Assert.notNull(user, "user is required");
        String hql = "from Duty duty where duty.user.id = ? and state = ? ";
        List<Duty> list = getSession().createQuery(hql).setParameter(0, user.getId()).setParameter(1, BaseEnumManager.StateEnum.Enable).list();

        return list;
    }

    @Override
    public List<Duty> getDutys(Department department) {
        Assert.notNull(department, "department is required");
        String hql = "from Duty duty where duty.power.department.id = ? and state = ? ";
        return getSession().createQuery(hql).setParameter(0, department.getId()).setParameter(1, BaseEnumManager.StateEnum.Enable).list();
    }

    @Override
    public List<Duty> getDutys(Department department, User user) {
        Assert.notNull(department, "department is required");
        Assert.notNull(user, "user is required");
        String hql = "from Duty duty where duty.power.department.id = ? and duty.user.id = ? and state = ? ";
        List<Duty> list = getSession().createQuery(hql).setParameter(0, department.getId()).setParameter(1, user.getId()).setParameter(2, BaseEnumManager.StateEnum.Enable).list();
        return list;
    }

    @Override
    public List<Department> getPrincipalDepartments(User user) {
        Assert.notNull(user, "user is required");
        String hql = "from Duty duty where duty.user.id = ? and state = ? and duty.dutyState = ? ";
        List<Duty> list = getSession().createQuery(hql).setParameter(0, user.getId()).setParameter(1, BaseEnumManager.StateEnum.Enable).setParameter(2, EnumManage.DutyState.Principal).list();
        List<Department> dList = new ArrayList<Department>();
        if(CollectionUtils.isEmpty(list) || list.size()>0){
            return  dList;
        }
        for(Duty duty:list){
            if(duty.getPower() != null){
                dList.add(duty.getPower().getDepartment());
            }
        }
        return dList;
    }

    @Override
    public List<User> getPersons(Department department) {
        Assert.notNull(department, "department is required");
        String hql = "from Duty duty where duty.department.id = ? and duty.state = ?";
        List<Duty> list = getSession().createQuery(hql).setParameter(0, department.getId()).setParameter(1, BaseEnumManager.StateEnum.Enable).list();
        List<User> usersList = new ArrayList<User>();
        if(CollectionUtils.isEmpty(list) || list.size()==0){
            return  usersList;
        }
        for(Duty duty:list){
            if(duty.getUser()!=null&&!usersList.contains(duty.getUser())){
                usersList.add(duty.getUser());
            }
        }
        return usersList;
    }

    @Override
    public List<User> getPersons(Department department, Post post) {
        Assert.notNull(department, "department is required");
        Assert.notNull(post, "post is required");
        String hql = "from Duty duty where duty.power.department.id = ? and duty.power.post.id = ? and state = ? ";
        List<Duty> list = getSession().createQuery(hql).setParameter(0, department.getId()).setParameter(1, post.getId()).setParameter(2, BaseEnumManager.StateEnum.Enable).list();
        List<User> usersList = new ArrayList<User>();
        if(list!=null&&list.size()>0){
            for(Duty duty:list){
                if(duty.getUser()!=null&&!usersList.contains(duty.getUser())){
                    usersList.add(duty.getUser());
                }
            }
        }
        return usersList;
    }

    @Override
    public User getPrincipal(Department department) {
        Assert.notNull(department, "department is required");
        String hql = "from Duty duty where duty.department.id = ? and duty.dutyState = ? and duty.state = ? ";
        List<Duty> list = getSession().createQuery(hql).setParameter(0, department.getId()).setParameter(1, EnumManage.DutyState.Principal).setParameter(2, BaseEnumManager.StateEnum.Enable).list();
        if(list!=null&&list.size()>0){
            Duty duty = list.get(0);
            if(duty.getUser()!=null){
                return duty.getUser();
            }
        }
        return null;
    }

    @Override
    public Duty getParentPrincipal(Department department) {
        //用户上级部门负责人
        Assert.notNull(department, "department is required");
        String hql="from Duty as du where du.power.department=(select dp.parent from Department as dp where dp=:m) and du.dutyState=:d and du.state=:s";
        List<Duty> dutyList = getSession().createQuery(hql).setParameter("m", department).setParameter("d", EnumManage.DutyState.Principal).setParameter("s", BaseEnumManager.StateEnum.Enable).list();
        if(dutyList.size() == 0){
            return null;
        }
        return dutyList.get(0);
    }



}
